<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");

class Category extends MY_Model {

	function __construct() {
		parent::__construct();
		$this->tableName = "category";
		$this->prefix = "cat";
	}

	function getCategory($where = NULL, $start = NULL, $limit = NULL, $fields = NULL, $query = NULL, $order = "catOrder", $dir = "ASC") {

		$fieldsQuery = Common::fieldsquery($fields, $query);
		$order = Common::coalesce(array($order, "catOrder"));
		$dir = Common::coalesce(array($dir, "ASC"));

		$qs =	"
					select
						cat_id, cat_name, cat_type, cat_length, type.szo_lname as cat_type_lname, cat_entity, entity.szo_lname as cat_entity_lname, cat_order, cat_list, cat_required, cat_highlight
					from
						".$this->db->dbprefix.$this->tableName."
						left join ".$this->db->dbprefix."szotar as type on cat_type = type.szo_sname and type.szo_type = 'CATTYPE' and type.szo_del = 0
						left join ".$this->db->dbprefix."szotar as entity on cat_entity = entity.szo_sname and entity.szo_type = 'ENTITY' and entity.szo_del = 0
					where
						cat_del = 0".((!is_null($where))?(" and ".$where):(""))." ".(($fieldsQuery)?(" and (".$fieldsQuery.") "):(""))."
					order by
						".((Common::underscore($order) == "cat_order")?("entity.szo_lname, cat_order"):(Common::underscore($order)))." ".$dir."
					".((is_numeric($start))?(" limit ".$start.",".$limit):(""))."
					";
		$result = $this->db->query($qs);

		$getCategory = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {

				$getCategory[$row->cat_entity][] = array(
					"catId" => $row->cat_id,
					"catName" => $row->cat_name,
					"catType" => $row->cat_type,
					"catLength" => $row->cat_length,
					"catTypeLname" => $row->cat_type_lname,
					"catEntity" => $row->cat_entity,
					"catEntityLname" => $row->cat_entity_lname,
					"catOrder" => $row->cat_order,
					"catList" => $row->cat_list,
					"catRequired" => $row->cat_required,
					"catHighlight" => $row->cat_highlight,
				);

			}
		}

		return $getCategory;
	}

	function getOrderToInsert($where) {
		$qs =	"
					select
						max(cat_order) as max
					from
						".$this->db->dbprefix.$this->tableName."
					where
						cat_del = 0".((!is_null($where))?(" and ".$where):(""))."
					";
		$result = $this->db->query($qs)->result();
		return $result[0]->max + 1;
	}

	function getCategoryVal($entity, $entId) {
		$qs =	"
					select
						cat_id, cat_name, cat_type, cat_length, cat_required, cat_list, e2c_ent_id, e2c_temp, e2c_temp_flag, val
					from
						".$this->db->dbprefix."category
						left join 
						(
							select
								e2c_ent_id, e2c_cat_id, coalesce(e2c_int, e2c_text) as val, e2c_temp, e2c_temp_flag
							from
								".$this->db->dbprefix."ent2cat
							where
								e2c_entity = '".$this->db->escape_str($entity)."' and e2c_ent_id = '".$this->db->escape_str($entId)."' and e2c_del = 0
						) as t on t.e2c_cat_id = cat_id
					where
						cat_entity = '".$this->db->escape_str($entity)."' and cat_del = 0
					order by
						cat_order
					";
		$result = $this->db->query($qs);
		$getCategoryVal = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				$getCategoryVal[$row->cat_id]["catName"] = $row->cat_name;
				$getCategoryVal[$row->cat_id]["catType"] = $row->cat_type;
				$getCategoryVal[$row->cat_id]["catLength"] = $row->cat_length;
				$getCategoryVal[$row->cat_id]["catRequired"] = $row->cat_required;
				$getCategoryVal[$row->cat_id]["catList"] = $row->cat_list;
				$getCategoryVal[$row->cat_id]["val"] = $row->val;
				$getCategoryVal[$row->cat_id]["e2cTemp"] = $row->e2c_temp;
				$getCategoryVal[$row->cat_id]["e2cTempFlag"] = $row->e2c_temp_flag;
			}
		}
		return $getCategoryVal;
	}

}